/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package anttasks.net;

import java.io.IOException;
import java.io.InputStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 *
 * @author hue
 */
public class VPN extends Task {
    private String login;
    private String pwd;
    
    public void setLogin(String login) {
        this.login = login;
    }
    
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    
    @Override
    public void execute() throws BuildException {
        try {
            
        }
        catch(Exception ex) {
            throw new BuildException(ex);
        }
    }
    
    public boolean openVPN(String vpnIp) {
        try {
            int iReturn = exec("rasdial Test " + login + " " + pwd + " /phone:" + vpnIp);
            if(iReturn != 0) {
                return false;
            }
        }
        catch(Exception ex) {
            System.out.println("checkIp:" + ex);
            return false;
        }
        return true;
    }
    
    private int exec(String command) throws IOException {
        Process p = Runtime.getRuntime().exec(command);
        SystemOut(p.getInputStream());

        return p.exitValue();
    }
    
    private void SystemOut(InputStream in) throws IOException {
        int nByte;
        final byte[] buff = new byte[1024*1024];
        while((nByte = in.read(buff)) > 0) {
            System.out.write(buff,0, nByte);
        }
        in.close();
    }
}
